function split=fbsplit( avi, backgroundfactor );
% split=fbsplit( avi, backgorundfactor ); creates grayscale movie with
% splited background from foreground. background and foreground uses other
% colors to paint

% init
split = [];
% first get initial background
%background = lumin( avi(1).cdata );
background = double( avi(1).cdata ) / 256;

% get frame num
frames = size( avi, 2 );

% treshold between background and foreground
treshold = 0.3;

for i = 1:frames
	% progress indicator
	disp( sprintf( '%d/%d', i, frames ) );
	
	% frame in grayscale
	grayframe = rgb2gray( avi(i).cdata );
	
	% diff btween current frame and bkgnd
	
	% luminance dofference
	%currentframe = lumin( grayframe );
	%bkgnddiff = abs( background - currentframe );
	
	% color difference
	currentframe = double( avi(i).cdata ) / 256;
	bkgnddiff = sum( abs( currentframe - background ), 3 ) / 3 
	
	% fopreground / background
	fb = bkgnddiff > treshold;
	
	% apply differences to frame
	%red = fb .* double( grayframe(:,:,1) );
	red = fb * 255;
	grayframe(:,:,1) = uint8( red );
	
	% update background
	background = background* (1-backgroundfactor) + double(currentframe) * backgroundfactor;
	
	% add frame
	newframe.cdata = grayframe;
	newframe.colormap = avi(i).colormap;
	
	split = [split newframe];
	
end
	